package main

import (
	"bytes"
	"crypto/aes"
	"encoding/hex"
	"fmt"
	"os"
	"syscall"

	"golang.org/x/term"
)

const (
	key = "f8*2/h&e3^8%$qp@#39j+c=9-[\"\x08h\x09]\xef"
)

func main() {
	var (
		username string
		buff     []byte = make([]byte, 64)
		rawPwd   []byte = make([]byte, 64)
	)
	fmt.Println("Please enter your sutdent id: ")
	fmt.Scan(&username)
	fmt.Printf("Please enter the password for [%s]: \n", username)

	pwd, err := term.ReadPassword(int(syscall.Stdin))
	if err != nil {
		panic(err)
	}

	fmt.Printf("Please enter the password again for [%s]: \n", username)
	_pwd, err := term.ReadPassword(int(syscall.Stdin))
	if err != nil {
		panic(err)
	}

	if !bytes.Equal(_pwd, pwd) {
		fmt.Println("you type different password!")
		return
	}
	copy(rawPwd, pwd)

	c, err := aes.NewCipher([]byte(key))
	if err != nil {
		panic(err)
	}
	c.Encrypt(buff, rawPwd)
	fd, err := os.Create("login.json")
	if err != nil {
		panic(err)
	}
	defer fd.Close()
	fd.WriteString(fmt.Sprintf("{\n    \"username\": \"%s\",\n    \"password\": \"%s\"\n}", username, hex.EncodeToString(buff)))
}
